package com.example.yiwei.crm.view.Class.listviewClass;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.entity.CustomerMessageEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by DHN on 2017/8/2.
 */

public class JDCustomerAdapter extends BaseAdapter{

    private Context context;
    public static List<CustomerMessageEntity> beans;
    // 用来控制CheckBox的选中状况
    private static HashMap<Integer, Boolean> isSelected;

    class ViewHolder {

        TextView tvName;
        TextView tvSex;
        TextView tvAge;
    }

    public JDCustomerAdapter(Context context, List<CustomerMessageEntity> beans) {
        // TODO Auto-generated constructor stub
        this.beans = beans;
        this.context = context;
        isSelected = new HashMap<Integer, Boolean>();
        // 初始化数据
        initDate();
    }


    // 初始化isSelected的数据
    public static void initDate() {
        for (int i = 0; i < beans.size(); i++) {
            getIsSelected().put(i, false);
        }
    }
    public int getCount() {
        // TODO Auto-generated method stub
        return beans.size();
    }
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return beans.get(position);
    }
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        // 页面
        ViewHolder holder;
        CustomerMessageEntity bean = beans.get(position);
        LayoutInflater inflater = LayoutInflater.from(context);
        if (convertView == null) {
            convertView = inflater.inflate(
                    R.layout.layout_item_jd_customer, null);
            holder = new ViewHolder();
            holder.tvName = (TextView) convertView
                    .findViewById(R.id.tv_device_name);
            holder.tvSex = (TextView) convertView.findViewById(R.id.tv_device_sex);
            holder.tvAge = (TextView) convertView.findViewById(R.id.tv_device_age);
            convertView.setTag(holder);
        } else {
            // 取出holder
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvName.setText( bean.getClient_name());
        if (bean.getClient_gender()==1){
            holder.tvSex.setText("男");
        }else {
            holder.tvSex.setText("女");
        }
        holder.tvAge.setText(bean.getClient_age()+"");

        // 监听checkBox并根据原来的状态来设置新的状态

        if (getIsSelected().get(position)!=null){

        }else {
            getIsSelected().put(position, false);
        }
        // 根据isSelected来设置checkbox的选中状况
        return convertView;
    }

    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
    }
    public static List<Integer> getIDs(){
        List<Integer> list  = new ArrayList<>();
        for (int i = 0;i<beans.size();i++){
            if (getIsSelected().get(i)!=null){

            }else {
                getIsSelected().put(i, false);
            }
            if(isSelected.get(i)){
                list.add(beans.get(i).getClient_id());
            }
        }
        return list;
    }
    public static void setState(){
        for (int i = 0;i<beans.size();i++){
            if(isSelected.get(i)){
                isSelected.put(i, false);
            }
        }
    }
}
